package View;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;

import Model.Authorization;
import Model.PlayersRating;
import Model.TeamsRating;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import prjClasses.User;

import java.awt.Color;

public class MainWindow {

	private JFrame frmSportPlanet;
	private JTextField textField_Pass;
	private JTextField textField_Login;
	private JLabel label_authorization;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow window = new MainWindow();
					window.frmSportPlanet.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public MainWindow() {
		initialize();
	}

	private void initialize() {
		frmSportPlanet = new JFrame();
		frmSportPlanet.setTitle("Sport Planet");
		frmSportPlanet.setBounds(100, 100, 537, 210);
		frmSportPlanet.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JButton btnNewButton = new JButton("Get Players Rating");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				createPlayersRating();
			}
		});

		JButton btnNewButton_1 = new JButton("Get Teams Rating");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				createTeamsRating();
			}
		});

		JPanel panelAuthorization = new JPanel();
		panelAuthorization.setBorder(new LineBorder(new Color(0, 0, 0)));

		label_authorization = new JLabel(
				"Please, enter your login and password.");

		JLabel label_1 = new JLabel("Password");

		JLabel label_2 = new JLabel("Login");

		textField_Pass = new JTextField();
		textField_Pass.setColumns(10);

		textField_Login = new JTextField();
		textField_Login.setColumns(10);

		JButton button_Authorize = new JButton("Enter");
		button_Authorize.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				authorize();
			}
		});
		GroupLayout gl_panelAuthorization = new GroupLayout(panelAuthorization);
		gl_panelAuthorization
				.setHorizontalGroup(gl_panelAuthorization
						.createParallelGroup(Alignment.TRAILING)
						.addGap(0, 266, Short.MAX_VALUE)
						.addGroup(
								gl_panelAuthorization
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panelAuthorization
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																label_authorization,
																GroupLayout.PREFERRED_SIZE,
																238,
																GroupLayout.PREFERRED_SIZE)
														.addGroup(
																gl_panelAuthorization
																		.createSequentialGroup()
																		.addGroup(
																				gl_panelAuthorization
																						.createParallelGroup(
																								Alignment.LEADING,
																								false)
																						.addComponent(
																								label_1,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								label_2,
																								GroupLayout.DEFAULT_SIZE,
																								56,
																								Short.MAX_VALUE))
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addGroup(
																				gl_panelAuthorization
																						.createParallelGroup(
																								Alignment.TRAILING,
																								false)
																						.addComponent(
																								textField_Pass,
																								Alignment.LEADING)
																						.addComponent(
																								textField_Login,
																								Alignment.LEADING)
																						.addComponent(
																								button_Authorize,
																								Alignment.LEADING,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE))))
										.addContainerGap(18, Short.MAX_VALUE)));
		gl_panelAuthorization
				.setVerticalGroup(gl_panelAuthorization
						.createParallelGroup(Alignment.LEADING)
						.addGap(0, 137, Short.MAX_VALUE)
						.addGroup(
								gl_panelAuthorization
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(label_authorization)
										.addGap(21)
										.addGroup(
												gl_panelAuthorization
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_2)
														.addComponent(
																textField_Login,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(12)
										.addGroup(
												gl_panelAuthorization
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_1)
														.addComponent(
																textField_Pass,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addComponent(button_Authorize)
										.addContainerGap(
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));
		panelAuthorization.setLayout(gl_panelAuthorization);
		GroupLayout groupLayout = new GroupLayout(
				frmSportPlanet.getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(
				Alignment.TRAILING).addGroup(
				Alignment.LEADING,
				groupLayout
						.createSequentialGroup()
						.addContainerGap()
						.addComponent(panelAuthorization,
								GroupLayout.PREFERRED_SIZE, 250,
								GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addGroup(
								groupLayout
										.createParallelGroup(Alignment.LEADING)
										.addComponent(btnNewButton,
												GroupLayout.DEFAULT_SIZE, 253,
												Short.MAX_VALUE)
										.addComponent(btnNewButton_1,
												GroupLayout.DEFAULT_SIZE, 253,
												Short.MAX_VALUE))
						.addContainerGap()));
		groupLayout
				.setVerticalGroup(groupLayout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								groupLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addComponent(
																				btnNewButton,
																				GroupLayout.PREFERRED_SIZE,
																				59,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				ComponentPlacement.RELATED,
																				13,
																				Short.MAX_VALUE)
																		.addComponent(
																				btnNewButton_1,
																				GroupLayout.PREFERRED_SIZE,
																				65,
																				GroupLayout.PREFERRED_SIZE)
																		.addContainerGap(
																				29,
																				Short.MAX_VALUE))
														.addGroup(
																Alignment.TRAILING,
																groupLayout
																		.createSequentialGroup()
																		.addComponent(
																				panelAuthorization,
																				GroupLayout.DEFAULT_SIZE,
																				155,
																				Short.MAX_VALUE)
																		.addContainerGap()))));
		frmSportPlanet.getContentPane().setLayout(groupLayout);
	}

	private void createPlayersRating() {
		PlayersRating rating = new PlayersRating();
		rating.createRatingPlayersChart();
	}

	private void createTeamsRating() {
		TeamsRating rating = new TeamsRating();
		rating.createRatingTeamsChart();
	}

	private void authorize() {
		String login = textField_Login.getText();
		String pass = textField_Pass.getText();

		Authorization auth = new Authorization();
		boolean authorization = auth.authorization(login, pass);
		//System.out.println(authorization);
		if (authorization) {
			User user = auth.authorizationGetUser(login, pass);
			String userFirstName = user.getFirstname();
			String userLastName = user.getLastname();
			String greeting = "Welcome, " + userFirstName + " " + userLastName
					+ "!";
			label_authorization.setText(greeting);
		}
		// TODO
	}
}
